// 更新昵称弹出层的内容
<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar
      title="设置昵称"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('close')"
      @click-right="updateName"
    />
    <!-- 输入框 -->
    <div class="field-wrap">
      <van-field
        v-model.trim="localName"
        rows="2"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入昵称"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
import { updateUserProfile } from "@/api/user";
export default {
  props: {
    value: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      localName: this.value,
    };
  },
  methods:{
    async  updateName(){
        this.$toast.loading({
            message:'保存中.......',
            forbidClick:true //禁止点击
        })
        try{
            const localName=this.localName
            if(!localName.length){
                this.$toast('昵称不能为空')
                return
            }
            await updateUserProfile({
                name:localName
            })
            // 更新视图
            this.$emit('input',localName)
            // 关闭弹层
            this.$emit('close')
            // 提示成功
            this.$toast.success('更新成功')
        }catch(err){
            // 更新失败
            this.$toast.fail('更新失败')
            console.log(err)
        }
    }
  }
};
</script>

<style lang="less" scoped>
.field-wrap {
  padding: 20px;
}
</style>